package com.heiyu;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;

public class Generator {

    private static String className ="com.heiyu.entity.HotelSheshiRef";

    private static String path = "/Users/mountain/Desktop/trip/service/src/main/java/com.heiyu/";

    private static String controllerPath = "/Users/mountain/Desktop/trip/admin/src/main/java/com.heiyu/controller/";


    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName(className);
        String modelName = clazz.getName().substring(clazz.getName().lastIndexOf(".")+1)+"Model";
        Field[] fields = clazz.getDeclaredFields();
        File file = new File(path+"model/"+modelName+".java");
        File serviceFile = new File(path+"service/"+modelName.substring(0,modelName.length()-5)+"Service.java");
        File serviceImplFile = new File(path+"service/impl/"+modelName.substring(0,modelName.length()-5)+"ServiceImpl.java");
        File controllerFile = new File(controllerPath+modelName.substring(0,modelName.length()-5)+"Controller.java");
        StringBuilder properties = new StringBuilder();
        StringBuilder getSet = new StringBuilder();
        StringBuilder conver = new StringBuilder();
        StringBuilder reconver = new StringBuilder();
        String type = "";
        String name = "";
        for(Field f:fields){
            if(f.getName().equals("serialVersionUID")){
                continue;
            }
            type = f.getType().getName().substring(f.getType().getName().lastIndexOf(".")+1);
            name = f.getName();
            properties.append("private "+(type.equals("Date")?"String":type)+"  "+f.getName()+";\n");
            getSet.append("public void set"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1)+"("+(type.equals("Date")?"String":type)+" "+f.getName()+")\n{\n this."+f.getName()+"="+f.getName()+";\n}\n");
            getSet.append("\npublic "+(type.equals("Date")?"String":type)+" get"+name.substring(0,1).toUpperCase()+name.substring(1)+"(){ \nreturn this."+f.getName()+";\n}\n");
            if(type.equals("Date")){
                conver.append("this."+name+"=DateUtils.format(entity.get"+name.substring(0,1).toUpperCase()+name.substring(1)+"(),null);\n");
                reconver.append("entity.set"+name.substring(0,1).toUpperCase()+name.substring(1)+"(DateUtils.parseDate("+name+",null));\n");
            }else{
                conver.append("this."+name+"=entity.get"+name.substring(0,1).toUpperCase()+name.substring(1)+"();\n");
                reconver.append("entity.set"+name.substring(0,1).toUpperCase()+name.substring(1)+"("+name+");\n");
            }
        }
        String data  = tmp.replaceAll("nxClassnx",className);
        data =  data.replaceAll("nxClassNamenx",modelName.substring(0,modelName.length()-5));
        data = data.replaceAll("nxModelnx",modelName);
        data = data.replaceAll("nxpropertiesnx",properties.toString());
        data = data.replaceAll("getset",getSet.toString());
        data = data.replaceAll("nxconvernx",conver.toString());
        data = data.replaceAll("nxreconvernx",reconver.toString());
        createFile(file,data);
        createFile(serviceFile,service.replaceAll("nxClassNamenx",modelName.substring(0,modelName.length()-5)).replaceAll("nxModelnx",modelName).replaceAll("nxServiceNamenx",modelName.substring(0,modelName.length()-5)));
        createFile(serviceImplFile,serviceImpl.replaceAll("nxClassNamenx",modelName.substring(0,modelName.length()-5)).replaceAll("nxModelnx",modelName).replaceAll("nxServiceNamenx",modelName.substring(0,modelName.length()-5)));
        createFile(controllerFile,controller.replaceAll("nxClassNamenx",modelName.substring(0,modelName.length()-5)).replaceAll("nxModelnx",modelName).replaceAll("nxServiceNamenx",modelName.substring(0,modelName.length()-5)).replaceAll("nxSmallClassNamenx",modelName.substring(0,1).toLowerCase()+modelName.substring(1,modelName.length()-5)));
    }

    public static void createFile(File file,String data) throws Exception {
        if(file.exists()){
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data.getBytes());
        fos.flush();
        fos.close();
    }

    private static String controller = "package com.heiyu.controller;\n" +
            "\n" +
            "import com.heiyu.entity.nxClassNamenx;\n" +
            "import com.heiyu.exception.BussinessException;\n" +
            "import com.heiyu.model.nxModelnx;\n" +
            "import com.heiyu.response.BaseResponse;\n" +
            "import com.heiyu.service.nxClassNamenxService;\n" +
            "import com.heiyu.utils.BaseController;\n" +
            "import org.springframework.beans.factory.annotation.Autowired;\n" +
            "import org.springframework.web.bind.annotation.ModelAttribute;\n" +
            "import org.springframework.web.bind.annotation.RequestMapping;\n" +
            "import org.springframework.web.bind.annotation.RequestParam;\n" +
            "import org.springframework.web.bind.annotation.RestController;\n" +
            "\n" +
            "import java.util.Map;\n" +
            "\n" +
            "@RestController\n" +
            "@RequestMapping(\"nxSmallClassNamenx\")\n" +
            "public class nxClassNamenxController extends BaseController{\n" +
            "        @Autowired\n" +
            "        private nxClassNamenxService nxSmallClassNamenxService;\n" +
            "\n" +
            "        @RequestMapping(\"put\")\n" +
            "        public Object put(@ModelAttribute(\"request\")nxModelnx request){\n" +
            "            if(!checkParams()){\n" +
            "                return miss(null);\n" +
            "            }\n" +
            "            if(this.nxSmallClassNamenxService.insert(request)){\n" +
            "                return success(null);\n" +
            "            }\n" +
            "            return fail(null);\n" +
            "        }\n" +
            "\n" +
            "        @RequestMapping(\"list\")\n" +
            "        public Object list( @RequestParam(required = false)Map<String,Object> request){\n" +
            "            try {\n" +
            "                request = this.nxSmallClassNamenxService.page(request);\n" +
            "            } catch (Exception e) {\n" +
            "                e.printStackTrace();\n" +
            "                return errror(null);\n" +
            "            }\n" +
            "            if(request == null){\n" +
            "                return fail(null);\n" +
            "            }\n" +
            "            BaseResponse response = success(null);\n" +
            "            response.setData(request);\n" +
            "            return response;\n" +
            "        }\n" +
            "\n" +
            "        @RequestMapping(\"get\")\n" +
            "        public Object get(@ModelAttribute(\"request\")nxModelnx request){\n" +
            "            nxClassNamenx info = this.nxSmallClassNamenxService.get(request);\n" +
            "            if(info == null){\n" +
            "                return empty(null);\n" +
            "            }\n" +
            "            request.conver(info);\n" +
            "            BaseResponse response = success(null);\n" +
            "            response.setData(request);\n" +
            "            return response;\n" +
            "        }\n" +
            "\n" +
            "        @RequestMapping(\"del\")\n" +
            "        public Object del(@RequestParam(\"id\")Long id){\n" +
            "            if(this.nxSmallClassNamenxService.del(id)){\n" +
            "                return success(null);\n" +
            "            }\n" +
            "            return fail(null);\n" +
            "        }\n" +
            "\n" +
            "        @RequestMapping(\"update\")\n" +
            "        public Object update(@ModelAttribute(\"request\")nxModelnx model){\n" +
            "            if(!checkParams(model.getId())){\n" +
            "                return miss(null);\n" +
            "            }\n" +
            "            try {\n" +
            "                if(this.nxSmallClassNamenxService.update(model)){\n" +
            "                    return  success(null);\n" +
            "                }\n" +
            "            } catch (BussinessException e) {\n" +
            "                e.printStackTrace();\n" +
            "                return errror(null);\n" +
            "            }\n" +
            "            return fail(null);\n" +
            "        }\n" +
            "    }\n";

    private static String service = "package com.heiyu.service;\n" +
            "\n" +
            "import com.heiyu.entity.nxClassNamenx;\n" +
            "import com.heiyu.model.nxModelnx;\n" +
            "\n" +
            "public interface nxServiceNamenxService extends BaseService<nxModelnx,nxClassNamenx>{\n" +
            "}\n";

    private static String serviceImpl = "package com.heiyu.service.impl;\n" +
            "\n" +
            "import com.heiyu.dao.nxClassNamenxMapper;\n" +
            "import com.heiyu.dao.BaseMapper;\n" +
            "import com.heiyu.entity.nxClassNamenx;\n" +
            "import com.heiyu.model.nxModelnx;\n" +
            "import com.heiyu.service.nxServiceNamenxService;\n" +
            "import org.springframework.beans.factory.annotation.Autowired;\n" +
            "import org.springframework.stereotype.Service;\n" +
            "\n" +
            "@Service\n" +
            "public class nxServiceNamenxServiceImpl extends BaseServiceImpl<nxModelnx,nxClassNamenx> implements nxServiceNamenxService{\n" +
            "\n" +
            "    @Autowired\n" +
            "    private nxServiceNamenxMapper baseMapper;\n" +
            "    @Override\n" +
            "    protected BaseMapper<nxClassNamenx> getMapper() {\n" +
            "        return baseMapper;\n" +
            "    }\n" +
            "}\n";

    private static String tmp = "package com.heiyu.model;\n" +
            "\n" +
            "import nxClassnx;\n\n" +
            "import com.heiyu.utils.DateUtils;" +
            "import com.heiyu.entity.BaseModel;\n" +
            "\n" +
            "public class nxModelnx extends BaseModel<nxClassNamenx>{\n" +
            "\n" +"nxpropertiesnx\n getset"+
            "    @Override\n" +
            "    public void conver(nxClassNamenx entity) {\n this.id = entity.getId();" +
            "\n" +"nxconvernx"+
            "    }\n" +
            "\n" +
            "    @Override\n" +
            "    public nxClassNamenx reconver() {\n" +"nxClassNamenx entity = new nxClassNamenx();\n entity.setId(id);"+
            "        nxreconvernx \nreturn entity;\n" +
            "    }\n" +
            "}\n";
}
